package com.haorui.lygyd.employeemanager.service;

import com.haorui.base.core.model.Search;
import com.haorui.base.core.service.BaseService;
import com.haorui.base.core.utils.BeanUtil;
import com.haorui.lygyd.employeemanager.dto.MemberDto;
import com.haorui.lygyd.employeemanager.dto.YuangongpingjiaDto;
import com.haorui.lygyd.employeemanager.entity.MemberEntity;
import com.haorui.lygyd.employeemanager.entity.YuangongpingjiaEntity;
import com.haorui.lygyd.employeemanager.repository.MemberRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created with IntelliJ IDEA.
 * 创建人: 系统生成
 * 日期: Sat Sep 02 20:04:29 CST 2017
 */
@Slf4j
@Service
public class MemberService extends BaseService<MemberEntity,String> {
    private MemberRepository memberRepository;
    private YuangongpingjiaService yuangongpingjiaService;
    public MemberService(@Autowired MemberRepository memberRepository) {
        super(memberRepository);
        this.memberRepository = memberRepository;
    }

    @Override
    protected Search<MemberEntity> getSearch() {
        return new Search<MemberEntity>(MemberEntity.class);
    }

    /**
     * 查看手机号码是否注册过
     *
     * @param shouJiHaoMa
     * @return
     */
    public boolean checkByShouJiHaoMa(String shouJiHaoMa) {
        return memberRepository.findMemberByShouJiHaoMa(shouJiHaoMa) != null;
    }
    /**
     * 查看身份证号是否注册过
     *
     * @param shengFenZhengHao
     * @return
     */
    public boolean checkByShengFenZhengHao(String shengFenZhengHao) {
        return memberRepository.findMemberByShengFenZhengHao(shengFenZhengHao) != null;
    }


    /**
     * 会员解锁锁定
     * @param dto
     * @return MemberEntity
     */
    public MemberEntity updateMemberJieSuoSuoDing(MemberDto dto) {
        if (dto.getId() == null) {
            return dto.buildPO();
        }
        MemberEntity dbEnetity = memberRepository.findOne(dto.getId());
        dbEnetity.setShiFouSuoDing(dto.getShiFouSuoDing());
        BeanUtil.beanCopyNotNull(dto, dbEnetity);
        memberRepository.save(dbEnetity);
        return dbEnetity;
    }

    /**
     * 会员批量锁定
     * @param memberIds
     * @return MemberEntity
     */

    public void suodingMember(String[] memberIds) {
        for (String memberId : memberIds    ) {
            MemberEntity memberEntity = memberRepository.findOne(memberId);
            memberEntity.setShiFouSuoDing(true);
            memberRepository.save(memberEntity);
        }
    }
    /**
     * 会员批量解锁
     * @param memberIds
     * @return MemberEntity
     */
    public void jiesuoMember(String[] memberIds) {
        for (String memberId : memberIds) {
            MemberEntity memberEntity = memberRepository.findOne(memberId);
            memberEntity.setShiFouSuoDing(false);
            memberRepository.save(memberEntity);
        }
    }

    /***
     * 验证支付密码是否正确
     * @param miMa
     * @return
     */
    public boolean checkByMemberMiMaAndId(String miMa,String id) {
        return memberRepository.findMemberByMiMaAndId(miMa,id) != null;
    }

    /**
     * 修改密码
     * @param dto
     * @return MemberEntity
     */
    public MemberEntity updateMemberPass(MemberDto dto) {
        if (dto.getId() == null) {
            return dto.buildPO();
        }
        MemberEntity dbEnetity = memberRepository.findOne(dto.getId());
        dbEnetity.setMiMa(dto.getNewMiMa());
        dto.setMiMa(dto.getNewMiMa());
        BeanUtil.beanCopyNotNull(dto, dbEnetity);
        memberRepository.save(dbEnetity);
        return dbEnetity;
    }

    /**
     * 员工评价
     * @param dto
     */
    public void pingjia(YuangongpingjiaDto dto)
    {
        YuangongpingjiaEntity yuangongpingjiaEntity = new YuangongpingjiaEntity();
        BeanUtil.beanCopyIgnoreDefault(yuangongpingjiaEntity, dto);
        yuangongpingjiaService.save(yuangongpingjiaEntity);
    }
}
